$(function() {
    var grid = $('#gridCustomer').datagrid({
        title: '客户信息',
        iconCls: 'icon-save',
        methord: 'post',
        url: '/System/GetCustomerList/',
        sortName: 'ID',
        sortOrder: 'desc',
        idField: 'ID',
        pageSize: 20,
        striped: true, //奇偶行是否区分
        frozenColumns: [[
	                { field: 'ck', checkbox: true }
				]],
        columns: [[
                    { field: 'TheName', title: '企业名称'},
					{ field: 'LegalPerson', title: '法人'},
					{ field: 'LPPhone', title: '法人联系电话'},
					{ field: 'RegisteredCapital', title: '注册资金'},
					{ field: 'Profit', title: '企业利润'},
					{ field: 'AnnualIncome', title: '年收入额'},
					{ field: 'FoundDate', title: '成立日期', formatter: $.getJsonDate},
					{ field: 'TaxNumber', title: '纳税人识别号'},
                    { field: 'Phone', title: '联系电话'},
                    { field: 'Contact', title: '联系人'},
					{ field: 'Addr', title: '地址'}
				]],
        fit: true,
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        singleSelect: false,
		onLoadError: function(data, status, error){
			if(data.status == "400")
				indexPage.reLogin();
		}
    });
    $('body').layout();
});

var CustomerManage = {};
CustomerManage.addForm = $("#addCustomerForm");
CustomerManage.DataGridAdd = function(){
	 $("#dlgCustomer").dialog({
		title: "添加客户信息",
		modal: true,
		resizable: true,
		width: 900,
		height: 248,
		toolbar: [{
					text: '保存',
					iconCls: 'icon-save',
					handler: function(){
						CustomerManage.CustomerAddFormSaveData();
					}
				}, '-', {
					text: '关闭',
					iconCls: 'icon-no',
					handler: function () {
						$("#dlgCustomer").dialog('close');
					}
				}],
		href: "/System/AddCustomer",
		onLoad: function(){
				    $("#addCustomerForm").form('clear');
			},
		onLoadError: function(data, status, error){
			if(data.status == "400")
				indexPage.reLogin();
		}
	});
}

CustomerManage.DataGridEdit = function(){
	var rows = $("#gridCustomer").datagrid('getSelections');
    var num = rows.length;
    if (num == 0) {
        $.messager.alert('提示:','请选择一条记录进行操作!','info');
        return;
    }
    else if (num > 1) {
        $.messager.alert('提示','只能选择一条记录进行操作!','info'); 
        return;
    }
    else {
		$("#dlgCustomer").dialog({
			title: "修改客户信息",
			modal: true,
			resizable: true,
			width: 900,
			height: 248,
			toolbar: [{
						text: '保存',
						iconCls: 'icon-save',
						handler: function(){
							CustomerManage.CustomerEditFormSaveData();
						}
					}, '-', {
						text: '关闭',
						iconCls: 'icon-no',
						handler: function () {
							$("#dlgCustomer").dialog('close');
						}
					}],
			href: "/System/AddCustomer",
			onLoad: function(){
				    $("#addCustomerForm").form('clear').form('load', rows[0]);
			},
			onLoadError: function(data, status, error){
				if(data.status == "400")
					indexPage.reLogin();
			}
		});
			
		}
}

CustomerManage.DataGridDel = function() {
    var ids = [];
    var rows = $("#gridCustomer").datagrid('getSelections');
    for (var i = 0; i < rows.length; i++) {
		ids.push(rows[i].ID);
    }
    if (ids.length > 0) {
        $.messager.confirm('提示', '您确认要删除选中的记录吗?', function (data) {
            if (data) {
                $.ajax({
                    url: '/System/DeleteCustomer?ids=' + ids.join(','),
                    type: 'post',
                    success: function (re) {
                        var data = eval('(' + re + ')');
                        if (data.success) {
                            $.messager.alert("提示",ids.length + "条记录" + data.msg,'info'); //提示消息
                            $("#gridCustomer").datagrid('reload');
                            $("#gridCustomer").datagrid('clearSelections'); //清除所有选中的元素
                        } else {
                            $.messager.alert('错误', data.msg, 'error');
                        }
                    },
					error: function(data, status, error){
						if(data.status == "400")
							indexPage.reLogin();
						else{
							$.messager.alert("错误", "删除失败!", "error");
                            $("#grid").datagrid("clearSelections");
						}
					}
                });
            }
        });
    } else {
        $.messager.alert("提示","请先选择要删除的记录。","info");
    }
}

CustomerManage.DataGridSearch = function(){
	var customer = $("#customerName").val();
	$("#gridCustomer").datagrid({ url: "/System/GetCustomerList/", queryParams: { customer: customer} });
}

CustomerManage.CustomerAddFormSaveData = function(){
	// $("#addCustomerForm").form('submit', {
        // url: 'System/SaveCustomer',
        // onSubmit: function () {
            // return $(this).form('validate');
        // },
        // success: function(data){
			// var data = eval('(' + data + ')');
			// if (data.success){
				// $.messager.alert('提示',data.msg,'info');
				// $("#addCustomerForm").form('clear');
				// $("#gridCustomer").datagrid('reload');
			// }
			// else{
				// $.messager.alert('错误',data.msg,'error');
			// }
		// }
    // });
	
	if($("#addCustomerForm").form('validate'))
	{
		$.ajax({
            type: "POST",   //post提交方式默认是get
            url: "System/SaveCustomer",
            data: $("#addCustomerForm").serialize(),   //序列化               
            error: function (data, status, error) {      // 设置表单提交出错                 
                if(data.status == "400")
				    indexPage.reLogin();
            },
            success: function (data) {
               var data = eval("(" + data + ")");
			    if (data.success){
				    $.messager.alert("提示",data.msg,"info");
				    $("#addCustomerForm").form("clear");
				    $("#gridCustomer").datagrid("reload");
			    }
			    else{
				    $.messager.alert("错误",data.msg,"error");
			    }
            }
        })
	}
}

CustomerManage.CustomerEditFormSaveData = function(){
	// $("#addCustomerForm").form('submit', {
        // url: 'System/EditCustomer',
        // onSubmit: function () {
            // return $(this).form('validate');
        // },
        // success: function(data){
			// var data = eval('(' + data + ')');
			// if (data.success){
				// $("#addCustomerForm").form('clear');
				// $("#gridCustomer").datagrid('reload');
				// $("#dlgCustomer").dialog('close');
				// $.messager.alert('提示',data.msg,'info');
			// }
			// else{
				// $.messager.alert('错误',data.msg,'error');
			// }
		// }
    // });
	
	if($("#addCustomerForm").form('validate'))
	{
		$.ajax({
            type: "POST",   //post提交方式默认是get
            url: "System/EditCustomer",
            data: $("#addCustomerForm").serialize(),   //序列化               
            error: function (data, status, error) {      // 设置表单提交出错                 
                if(data.status == "400")
				    indexPage.reLogin();
            },
            success: function (data) {
               var data = eval("(" + data + ")");
			    if (data.success){
				    $("#addCustomerForm").form('clear');
					$("#gridCustomer").datagrid('reload');
					$("#dlgCustomer").dialog('close');
					$.messager.alert('提示',data.msg,'info');
			    }
			    else{
				    $.messager.alert("错误",data.msg,"error");
			    }
            }
        })
	}
}